<?php

namespace App\Http\Controllers\Admin;

use App\Model\Cate;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Config;
use Intervention\Image\ImageManagerStatic as Image;

class CateController extends Controller
{
    // 上传分类图片
    public function upload(Request $request)
    {
        $file = $request->file('file');

        if(!$file->isValid()){
            return response()->json(['code'=>0,'message'=>'无效的上传文件']);
        }

        // 获取源文件扩展名
        $ext = $file->getClientOriginalExtension();

        // 新的文件名
        $newfile = md5(time().rand(1000,9999)) . $ext;

        // 文件上传指定路径
        $path = public_path('/uploads');

        // 将临时文件移动到目录文件夹
//        if(!$file->move($path,$newfile)){
//            return response()->json(['code'=>0,'message'=>'保存文件失败']);
//        }
//        $result = Image::make($file)->resize(100,100)->save($path.'/'.$newfile);

//        if($result){
//            // 成功
//            return response()->json(['code'=>1,'message'=>$newfile]);
//        }else{
//            return response()->json(['code'=>0,'message'=>'文件上传失败']);
//        }

        // 七牛云上传
        $qiniu = qiniuUpload($file);

        if(!empty($qiniu)){
            $urlImage = Config::get('filesystems.disks.qiniu.domain') . '/' . $qiniu . '?imageView/1/w/100/h/100';
            // 成功
            return response()->json(['code'=>1,'message'=>'上传成功','data'=>[
                'urlImage' => $urlImage,
                'qiniu' => $qiniu
            ]]);
        }else{
            return response()->json(['code'=>0,'message'=>'文件上传失败']);
        }

    }
    // 下载分类图标
    public function downLoad($key)
    {
        qiniuDownload($key);
    }
    // 修改排序
    public function changeOrder(Request $request)
    {
        // 获取数据
        $input = $request->except('_token');

        // 获取当前分类
        $cate = Cate::find($input['cate_id']);

        $result = $cate->update(['cate_order'=> $input['cate_order']]);

        if($result === FALSE){
            $data = [
                'code'=> 0,
                'message'=>'修改失败'
            ];
        }else{
            $data = [
                'code'=> 1,
                'message'=>'修改成功'
            ];
        }
        return $data;
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        // 获取get请求传递的参数
        $cate_list = (new Cate())->tree($request);

        return view('admin.cate.list',compact('cate_list','request'));
    }

    public function object_array($array) {
        if(is_object($array)) {
            $array = (array)$array;
        }
        if(is_array($array)) {
            foreach($array as $key=>$value) {
                $array[$key] = $this->object_array($value);
            }
        }
        return $array;
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        // 查询一级分类
        $cate = Cate::where(['cate_pid' => 0])->get();

        return view('admin.cate.add',compact('cate'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        // 1.获取用户添加的数据
        $input = $request->all();

        $cate_name = Cate::where('cate_name',$input['cate_name'])->first();

        if($cate_name){
            $data = [
                'code'=> 0,
                'message'=>'分类昵称已经存在'
            ];
            return $data;
        }

        // 封装插入数据库的数组
        $data = [
            'cate_name' => $input['cate_name'],
            'cate_title' => $input['cate_title'],
            'cate_order' => $input['cate_order'],
            'cate_pid' => $input['cate_pid'],
        ];

        // 验证
        $result = Cate::create($data);

        if($result === FALSE){
            $data = [
                'code'=> 0,
                'message'=>'添加失败'
            ];
        }else{
            $data = [
                'code'=> 1,
                'message'=>'添加成功'
            ];
        }
        return $data;
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
